package com.ruoyi.system.commons.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import lombok.extern.slf4j.Slf4j;

import java.util.Iterator;
import java.util.Set;

/**
 * @Auther Yulong Liu
 * @Date 2023/12/22 12:06
 * @Description
 **/

@Slf4j
public class JsonValidationUtils {
    public static boolean schemaValidation(String JsonSchema,String valiData){
        log.info("======json格式校验======");
        try {
            ObjectMapper mapper = new ObjectMapper();
            //指定版本draft-06
            JsonSchemaFactory schemaFactory = JsonSchemaFactory
                    .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V6))
                    .objectMapper(new ObjectMapper())
                    .build();
            //注册文件的JSONSchema
            JsonNode jsonSchema = mapper.readTree(JsonSchema);
            JsonSchema schema = schemaFactory.getSchema(jsonSchema);
            JsonNode jsonNode = mapper.readTree(valiData);
            Set<ValidationMessage> validate = schema.validate(jsonNode);
            if (!validate.isEmpty()) {//非空就是校验失败，这里打印出
                Iterator<ValidationMessage> iterator = validate.iterator();
                while (iterator.hasNext()){
                    ValidationMessage next = iterator.next();
                    log.info("接收到的请求",next.getMessage());
                }
                return false;
            }
            return true;
        }catch(Exception e ){
            e.printStackTrace();
            return false;
        }
    }

    public static final String REGISTRY_JSON_SCHEMA = "{\n" +
            "    \"type\": \"object\",\n" +
            "    \"properties\": {\n" +
            "        \"tool_name\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"tool_role\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"description\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"request_type\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"prompt\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"shots\": {\n" +
            "            \"type\": \"array\",\n" +
            "            \"items\": {\n" +
            "                \"type\": \"object\",\n" +
            "                \"properties\": {\n" +
            "                    \"query_input\": {\n" +
            "                        \"type\": \"string\"\n" +
            "                    },\n" +
            "                    \"expected_output\": {\n" +
            "                        \"type\": \"string\"\n" +
            "                    }\n" +
            "                }\n" +
            "            }\n" +
            "        },\n" +
            "        \"base_url\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"path\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"headers\": {\n" +
            "            \"type\": \"object\",\n" +
            "            \"properties\": {\n" +
            "                \"Content-Type\": {\n" +
            "                    \"type\": \"string\"\n" +
            "                }\n" +
            "            },\n" +
            "            \"required\": [\n" +
            "                \"Content-Type\"\n" +
            "            ]\n" +
            "        },\n" +
            "        \"params\": {\n" +
            "            \"type\": \"object\",\n" +
            "            \"properties\": {}\n" +
            "        },\n" +
            "        \"body\": {\n" +
            "            \"type\": \"object\",\n" +
            "            \"properties\": {\n" +
            "                \"type\": {\n" +
            "                    \"type\": \"string\"\n" +
            "                },\n" +
            "                \"properties\": {\n" +
            "                    \"type\": \"array\",\n" +
            "                    \"items\": {\n" +
            "                        \"type\": \"object\",\n" +
            "                        \"properties\": {\n" +
            "                            \"name\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            },\n" +
            "                            \"type\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            },\n" +
            "                            \"description\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            }\n" +
            "                        }\n" +
            "                    }\n" +
            "                },\n" +
            "                \"properties_json\": {\n" +
            "                    \"type\": \"string\"\n" +
            "                },\n" +
            "                \"required\": {\n" +
            "                    \"type\": \"array\",\n" +
            "                    \"items\": {\n" +
            "                        \"type\": \"object\",\n" +
            "                        \"properties\": {\n" +
            "                            \"paramName\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            },\n" +
            "                            \"paramPath\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            },\n" +
            "                            \"description\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            }\n" +
            "                        }\n" +
            "                    }\n" +
            "                }\n" +
            "            },\n" +
            "            \"required\": [\n" +
            "                \"type\",\n" +
            "                \"properties\",\n" +
            "                \"properties_json\",\n" +
            "                \"required\"\n" +
            "            ]\n" +
            "        },\n" +
            "        \"response_content\": {\n" +
            "            \"type\": \"object\",\n" +
            "            \"properties\": {\n" +
            "                \"type\": {\n" +
            "                    \"type\": \"string\"\n" +
            "                },\n" +
            "                \"properties\": {\n" +
            "                    \"type\": \"object\",\n" +
            "                    \"properties\": {}\n" +
            "                },\n" +
            "                \"required\": {\n" +
            "                    \"type\": \"array\",\n" +
            "                    \"items\": {\n" +
            "                        \"type\": \"object\",\n" +
            "                        \"properties\": {\n" +
            "                            \"resultName\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            },\n" +
            "                            \"description\": {\n" +
            "                                \"type\": \"string\"\n" +
            "                            }\n" +
            "                        }\n" +
            "                    }\n" +
            "                }\n" +
            "            },\n" +
            "            \"required\": [\n" +
            "                \"type\",\n" +
            "                \"properties\",\n" +
            "                \"required\"\n" +
            "            ]\n" +
            "        }\n" +
            "    },\n" +
            "    \"required\": [\n" +
            "        \"tool_name\",\n" +
            "        \"tool_role\",\n" +
            "        \"description\",\n" +
            "        \"request_type\",\n" +
            "        \"prompt\",\n" +
            "        \"shots\",\n" +
            "        \"base_url\",\n" +
            "        \"path\",\n" +
            "        \"headers\",\n" +
            "        \"params\",\n" +
            "        \"body\",\n" +
            "        \"response_content\"\n" +
            "    ]\n" +
            "}";

    public static final String TOOLKIT_JSON_SCHEMA = "{\n" +
            "    \"type\": \"object\",\n" +
            "    \"properties\": {\n" +
            "        \"tool_kit_name\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"tool_kit_role\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"description\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"intention\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"prompt_template\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"shots\": {\n" +
            "            \"type\": \"array\",\n" +
            "            \"items\": {\n" +
            "                \"type\": \"object\",\n" +
            "                \"properties\": {\n" +
            "                    \"query_input\": {\n" +
            "                        \"type\": \"string\"\n" +
            "                    },\n" +
            "                    \"expected_output\": {\n" +
            "                        \"type\": \"string\"\n" +
            "                    }\n" +
            "                }\n" +
            "            }\n" +
            "        },\n" +
            "        \"base_url\": {\n" +
            "            \"type\": \"string\"\n" +
            "        },\n" +
            "        \"provider_info\": {\n" +
            "            \"type\": \"object\",\n" +
            "            \"properties\": {\n" +
            "                \"logo_url\": {\n" +
            "                    \"type\": \"string\"\n" +
            "                },\n" +
            "                \"contact_email\": {\n" +
            "                    \"type\": \"string\"\n" +
            "                }\n" +
            "            },\n" +
            "            \"required\": [\n" +
            "                \"logo_url\",\n" +
            "                \"contact_email\"\n" +
            "            ]\n" +
            "        }\n" +
            "    },\n" +
            "    \"required\": [\n" +
            "        \"tool_kit_name\",\n" +
            "        \"tool_kit_role\",\n" +
            "        \"description\",\n" +
            "        \"intention\",\n" +
            "        \"base_url\"\n" +
            "    ]\n" +
            "}";
}
